/**
 * a class representing a Triangle in two dimentional space.
 * @author Nadav Cohen, Matan Monitz
 *
 */
public class Triangle extends Polygon {
	/**
	 * a constructor for triangle by using three instances of Point for corners 
	 * @param p1 an instance of Point. must be initialized
	 * @param p2 an instance of Point. must be initialized
	 * @param p3 an instance of Point. must be initialized
	 */
	public Triangle(Point p1, Point p2, Point p3)
	{
		super(new Point[] {p1,p2,p3});
	}
	/**
	 * a copy constructor for Triangle utilizing deep copy semantics
	 * @param t an instance of Triangle. must be initialized
	 */
	public Triangle(Triangle t) 
	{
		super(new Point[] {t.getP1(),t.getP2(),t.getP3()});
	}
	/**
	 * a getter for the first corner of the Triangle
	 * @return a new instance of Point equal to the first corner of the Triangle
	 */
	public Point getP1()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[0]);
	}
	/**
	 * a getter for the second corner of the Triangle
	 * @return a new instance of Point equal to the second corner of the Triangle
	 */
	public Point getP2()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[1]);
	}
	/**
	 * a getter for the third corner of the Triangle
	 * @return a new instance of Point equal to the third corner of the Triangle
	 */
	public Point getP3()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[2]);
	}
	/**
	 * perform a logical equality test for triangles
	 * @param other the object to be evaluated for equality
	 * @return true if the input object is an instance of Triangle and the corners are equal without regard to the order of the corners
	 */
	public boolean equals(Object other)
	{
		if(other instanceof Triangle)
		{
			return super.equals(other);
		}
		else
			return false;
	}
	/**
	 * calculates the area of the triangle using herons formula
	 * @return the area of the triangle
	 */
	public double getArea()
	{
		//using herons formula (d*(d-s1)*(d-s2)*(d-s3))^0.5
		//d= half the perimeter
		//s1,s2,s3 = sides of the triangle
		double D = this.getPerimeter()/2;
		return Math.pow(D*(D-this.getP1().distance(this.getP2()))*(D-this.getP1().distance(this.getP3()))*(D-this.getP2().distance(this.getP3())), 0.5);
	}
	/**
	 * checks if a given point is located inside the area of the triangle.
	 * @param P an instance of Point to be checked. must be initialized.
	 * @return true if the point is inside the triangle.<br> false if the point is outside the triangle. 
	 */
	public boolean contains(Point P)
	{
		//create three new triangles using pairs of points from the original triangle and the input point
		//if the sum of the areas of the three new triangles is smaller then the original triangle's area then the point is inside the shape
		Triangle tr1 = new Triangle(this.getP1(),this.getP2(),P);
		Triangle tr2 = new Triangle(this.getP1(),this.getP3(),P);
		Triangle tr3 = new Triangle(this.getP2(),this.getP3(),P);
		double sum=tr1.getArea()+tr2.getArea()+tr3.getArea();
		return (this.getArea()>=sum-0.0001 && this.getArea()<=sum+0.0001);
	}
}
